import 'package:mobile_app.core/core/domain/entities/learning_content/lexical_item/lexical_item.dart';

int compareLexicalItemsByFrequency(LexicalItem firstLexicalItem, LexicalItem secondLexicalItem) {
  switch (firstLexicalItem) {
    case InbuiltWord():
      switch (secondLexicalItem) {
        case InbuiltWord():
          return firstLexicalItem.rank.compareTo(secondLexicalItem.rank);
        case InbuiltPhrasalVerb():
          return 0;
        case CustomLexicalItem():
          return -1;
      }

    case InbuiltPhrasalVerb():
      switch (secondLexicalItem) {
        case InbuiltWord():
          return 0;
        case InbuiltPhrasalVerb():
          return 0;
        case CustomLexicalItem():
          return -1;
      }

    case CustomLexicalItem():
      switch (secondLexicalItem) {
        case InbuiltWord():
          return 1;
        case InbuiltPhrasalVerb():
          return 1;
        case CustomLexicalItem():
          return 0;
      }
  }
}

int compareLexicalItemsTitleAlphabetically(
    LexicalItem firstLexicalItem, LexicalItem secondLexicalItem) {
  return firstLexicalItem.title.toLowerCase().compareTo(secondLexicalItem.title.toLowerCase());
}

int compareLexicalItemsTranslationAlphabetically(
    LexicalItem firstLexicalItem, LexicalItem secondLexicalItem) {
  return firstLexicalItem.translation
      .toLowerCase()
      .compareTo(secondLexicalItem.translation.toLowerCase());
}

int compareLexicalItemsByPriority(LexicalItem firstLexicalItem, LexicalItem secondLexicalItem) {
  final firstLexicalItemPriority = _getLexicalItemPriority(firstLexicalItem);
  final secondLexicalItemPriority = _getLexicalItemPriority(secondLexicalItem);

  return firstLexicalItemPriority.compareTo(secondLexicalItemPriority);
}

int compareLexicalItemsByLearning(LexicalItem firstLexicalItem, LexicalItem secondLexicalItem) {
  final firstLexicalItemProgressTypeRank = _getLexicalItemProgressTypeRank(firstLexicalItem);
  final secondLexicalItemProgressTypeRank = _getLexicalItemProgressTypeRank(secondLexicalItem);

  if (firstLexicalItemProgressTypeRank < secondLexicalItemProgressTypeRank) {
    return -1;
  } else if (firstLexicalItemProgressTypeRank > secondLexicalItemProgressTypeRank) {
    return 1;
  }

  final firstLexicalItemProgress = firstLexicalItem.progress;
  final secondLexicalItemProgress = secondLexicalItem.progress;

  if (firstLexicalItemProgress is LearningLexicalItemProgress &&
      secondLexicalItemProgress is LearningLexicalItemProgress) {
    final firstRepetitionNumber = firstLexicalItemProgress.learningStages.last.repetitionNumber;
    final secondRepetitionNumber = secondLexicalItemProgress.learningStages.last.repetitionNumber;

    if (firstRepetitionNumber > secondRepetitionNumber) {
      return -1;
    } else if (firstRepetitionNumber < secondRepetitionNumber) {
      return 1;
    }
  }

  return 0;
}

int _getLexicalItemPriority(LexicalItem lexicalItem) {
  switch (lexicalItem) {
    case InbuiltNoun():
      return 1;
    case InbuiltAdjective():
      return 2;
    case InbuiltVerb():
      return 3;
    case InbuiltPhrasalVerb():
      return 4;
    case InbuiltNumeral():
      return 5;
    case InbuiltAdverb():
      return 6;
    case InbuiltPronoun():
      return 7;
    case InbuiltConjunction():
      return 8;
    case InbuiltPreposition():
      return 9;
    case CustomLexicalItem():
      return 10;
  }
}

int _getLexicalItemProgressTypeRank(LexicalItem lexicalItem) {
  if (lexicalItem.excluded) {
    return 3;
  }

  switch (lexicalItem.progress) {
    case null:
      return 2;
    case PreviouslyKnownLexicalItemProgress():
      return 0;
    case LearningLexicalItemProgress():
      return 1;
  }
}
